<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\Services;

import("Classes.Services.AbstractService");

use \PsiFramework\Applications\AlloMediaPortal\Classes\DAO\AlloMediaEntryDAO;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Services\AbstractService;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\Object\Date;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;
use \PsiFramework\Plugins\Form\Classes\DAO\FormResponseDAO;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormInput;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormText;
use \PsiFramework\Plugins\Form\Classes\Util\Json\Elements\JsonFormTitle;
use \PsiFramework\Classes\Util\Manager\ApplicationManager;
use \PsiFramework\Classes\Util\HTTP\HTTPRequest;

/**
 * Bibliotheque
 * Gestion de ma bibliothèque
 *
 * @author Rémi San
 * @version beta
 */

/**
 * Service de envoi de mise à jour d'une entrée
 *
 * @package Services
 */
class SendEntryUpdateService extends AbstractService
{

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * The main function of the method which is called every time
     * an service is created by the front controller.
     *
     * @param Request $request the HTTP request to be treated by the action
     * 
     * @return void
     */
    public function execute(Request $request)
    {
        parent::execute($request);

        $entryDao = DAOFactory::getDAO('AlloMediaEntry');
        $entry = $entryDao->retrieveByPk($request->getParameter('entry_id'));
        
        $ok = true;
        
        if (!is_null($entry)) {
        
        	$password = ApplicationManager::getInstance()->getApplication()->getParameter('xmlPassword');
        	$updateUrl =  ApplicationManager::getInstance()->getApplication()->getParameter('updateUrl');
        
        	$doc = new \DOMDocument('1.0', 'UTF-8');
        	$doc->formatOutput = true;
        	$entryXml = AlloMediaEntryDAO::getAsXml($entry, null, $doc);
        	$doc->appendChild($entryXml);
        
        	$sites = $entry->getSites();
        	foreach ($sites as $siteMembership) {
        		$idForSite = $siteMembership->getSiteUserId();
        		$site = $siteMembership->getSite();
        
        		$entryXml->setAttribute("id", $idForSite);
        
        		$entryXmlString = $doc->saveXML();
        		
        		$httpRequest
            		= new HTTPRequest(
        		        $site->getUri().$updateUrl
            		);
        		$httpRequest->addParameter('information', $entryXmlString);
        		$httpRequest->addParameter('securityKey', sha1($entryXmlString.$password));
        
        		try {
        			$response = $httpRequest->execute(HTTPRequest::$post);
        			if ($response != 'success') {
        			    $ok = false;
        			    break;
        			}
        		} catch (\Exception $e) {
        			echo 'Impossible to send the Request to update the entry : ' . $e->getMessage();
        			break;
        		}
        	}
        }
        
        if ($ok) {
            echo 'success';
        } else {
            echo 'error';
        }
    }
}
